package com.health.controller.setmeal;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.constant.MessageConstant;
import com.health.entity.FileResult;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Setmeal;
import com.health.service.SetmealService;
import com.health.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Classname SetmealController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/16 13:17
 * @Author RainGrd
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    private SetmealService setmealService;
   /* @Autowired
    private JedisPool jedisPool;*/

    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) throws IOException {
        try {
            FileResult fileResult = FileUtils.uploadFile(multipartFile, "", "jpg,png,jpeg");
            log.info("上传完成后的文件:{}",fileResult);
            log.info("上传完成后的文件名称:{}", fileResult.getFileName());
            //将上传图片名称存入Redis，基于Redis的Set集合存储
//            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileResult.getExtName());
            return Result.success( "../upload/" +fileResult.getFileName(), MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    /**
     * 用于保存预约管理
     *
     * @param setmeal
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        log.info("setmeal:{}", setmeal);
        log.info("checkGroupIds:{}", Arrays.asList(checkGroupIds));
        return setmealService.saveSetmeal(setmeal, checkGroupIds);
    }

    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.querySetmealPage(queryPageBean);
    }

    @RequestMapping("/findCheckGroupIdsBySetmealId.do")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId) {
        log.info("查询当前检查组包含的所有检查项id:{}", setmealId);
        return setmealService.queryCheckGroupIdsBySetmealId(setmealId);
    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        log.info("要编辑的检查组对象:{}", setmeal);
        log.info("要编辑的检查组对象数组:{}", Arrays.asList(checkGroupIds));
        return setmealService.editSetmeal(setmeal, checkGroupIds);
    }


    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        log.info("要删除的套餐id:{}", id);
        return setmealService.clearSetmealById(id);
    }

    @RequestMapping("/queryAll.do")
    public Result queryAll() {
        return setmealService.queryAll();
    }
}
